package site.marqstree.latte.ec.main.sort.list;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.main.sort.SortDelegate;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.sort.list
 * 文件名: VerticalListDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/29 13:19
 * 描述: 左侧列表
 */
public class VerticalListDelegate extends LatteDelegate {

    @BindView(R2.id.rv_vertical_menu_list)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_vertical_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        //初始化RecyclerView
        initRecyclerView();
    }

    //初始化RecyclerView
    private void initRecyclerView() {
        //列表布局
        final LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(manager);
        //屏蔽动画效果
        mRecyclerView.setItemAnimator(null);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        //请求分类数据
        RestClient.builder()
                .url(AppProfile.SORT_LIST_DATA_URL)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        VerticalListDataConverter converter = new VerticalListDataConverter();
                        final List<MultipleItemEntity> data =
                                converter.setJsonData(response).convert();
                        final SortDelegate delegate = getParentDelegate();
                        int contentId = converter.getGetCurrentContentId();
                        final SortRecyclerAdapter adapter = new SortRecyclerAdapter(data, delegate,contentId);
                        mRecyclerView.setAdapter(adapter);
                    }
                })
                .build()
                .get();
    }
}
